package com.leo.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 网络状态工具类
 * Created by benson on 14/12/24.
 */
public class NetworkUtils {

    private NetworkUtils() {
    }

    /**
     * 获取mac地址
     * @param context
     * @return
     */
    public static String getWifiAddress(Context context) {
        if(context != null) {
            PackageManager pm = context.getPackageManager();
            if (pm.checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, context.getPackageName()) != 0) {
                return "00-00-00-00-00-00";
            }

            WifiManager systemService = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (systemService != null) {
                WifiInfo wifiInfo = systemService.getConnectionInfo();
                if(wifiInfo != null) {
                    String mac = wifiInfo.getMacAddress();
                    if(StringUtils.isEmpty(mac)) {
                        return mac;
                    }
                }
            }
        }
        return "00-00-00-00-00-00";
    }

    /**
     * 联通，电信，移动
     * @param context
     * @return
     */
    public static String[] getISPType(Context context) {
        String[] ret = {"0", "unknow"};
        if (context != null) {
            String imsi = AndroidUtils.getImsi(context);
            if (!TextUtils.isEmpty(imsi)) {
                //因为移动网络编号46000下的IMSI已经用完，所以虚拟了一个46002编号，134/159号段使用了此编号
                if (imsi.startsWith("46000") || imsi.startsWith("46002")) {
                    //中国移动
                    ret[0] = "1";
                    ret[1] = "移动";
                } else if (imsi.startsWith("46001")) {
                    //中国联通
                    ret[0] = "2";
                    ret[1] = "联通";
                } else if (imsi.startsWith("46003")) {
                    //中国电信
                    ret[0] = "3";
                    ret[1] = "电信";
                }
            }
        }
        return ret;
    }


    /**
     * 获取当前网络类型
     *
     * @return 返回网络类型
     */
    public static NetworkType getNetworkType(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null || !info.isConnectedOrConnecting()) {
            return NetworkType.NONE;
        }
        int type = info.getType();
        if (ConnectivityManager.TYPE_WIFI == type) {
            return NetworkType.WIFI;
        } else if (ConnectivityManager.TYPE_MOBILE == type) {
            return NetworkType.MOBILE;
        } else {
            return NetworkType.OTHER;
        }
    }

    /**
     * 获取当前网络类型字符串
     *
     * @return 返回 WiFi,2G,3G,4G,Other
     */
    public static String getNetworkStr(Context mContext) {
        try {

            ConnectivityManager cm = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo info = cm.getActiveNetworkInfo();
            if (info == null || !info.isConnectedOrConnecting()) {
                return "Other";
            }
            int type = info.getType();
            if (ConnectivityManager.TYPE_WIFI == type) {
                return "WiFi";
            } else if (ConnectivityManager.TYPE_MOBILE == type) {
                int networkSubType = info.getSubtype();
                switch (networkSubType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        return "2G";
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        return "3G";
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        return "4G";
                    default:
                        return "Other";
                }
            } else {
                return "Other";
            }
        } catch (Exception e) {
        }
        return "Other";
    }

    /**
     * 获取当前网络类型制式的字符串
     *
     * @return 返回 LTE,CDMA,WCDMA 等等
     */
    public static String getNetworkSystemStr(Context mContext) {
        try {

            ConnectivityManager cm = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo info = cm.getActiveNetworkInfo();
            if (info == null || !info.isConnectedOrConnecting()) {
                return "Other";
            }
            int type = info.getType();
            if (ConnectivityManager.TYPE_WIFI == type) {
                return "WiFi";
            } else if (ConnectivityManager.TYPE_MOBILE == type) {
                int networkSubType = info.getSubtype();
                switch (networkSubType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    	return "GPRS";
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    	return "EDGE";
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    	return "CDMA";
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    	return "1xRTT";
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                    	return "IDEN";
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    	return "UMTS";
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    	return "EVDO_0";
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    	return "EVDO_A";
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    	return "HSDPA";
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    	return "HSUPA";
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    	return "HSPA";
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    	return "EVDO_B";
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    	return "EHRPD";
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                    	return "HSPAP";
                    case TelephonyManager.NETWORK_TYPE_LTE:
                    	return "LTE";
                    default:
                        return "Other";
                }
            } else {
                return "Other";
            }
        } catch (Exception e) {
        }
        return "Other";
    }
    
    /**
     * 网络连接是否断开
     *
     * @param context Context
     * @return 是否断开s
     */
    public static boolean isNotConnected(Context context) {
        return !isConnected(context);
    }

    /**
     * 是否有网络连接
     *
     * @param context Context
     * @return 是否连接
     */
    public static boolean isConnected(Context context) {
        if (context == null) {
            return true;
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return info != null && info.isConnectedOrConnecting();
    }

    /**
     * 当前是否是WIFI网络
     *
     * @param context Context
     * @return 是否WIFI
     */
    public static boolean isWifi(Context context) {
        return NetworkType.WIFI.equals(getNetworkType(context));
    }

    /**
     * 当前是否移动网络
     *
     * @param context Context
     * @return 是否移动网络
     */
    public static boolean isMobile(Context context) {
        return NetworkType.MOBILE.equals(getNetworkType(context));
    }

    public enum NetworkType {
        WIFI, MOBILE, OTHER, NONE
    }
}
